package me.bemayor.components.twin.items;

import me.bemayor.api.common.LoreBuilder;
import me.bemayor.api.customitem.CustomItemStack;
import me.bemayor.components.transition.items.HeadTexture;
import me.bemayor.components.twin.TwinUsage;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

public class PortableTwinBox extends CustomItemStack{
    public PortableTwinBox() {
        super("PORTABLE_TWIN_BOX",
                HeadTexture.PORTABLE_TWIN_BOX.getTexture(),
                "&6便携孪生箱",
                "&a&o打开对应的箱子容器", "箱子坐标", "", "", LoreBuilder.RIGHT_CLICK_TO_OPEN);
        this.setPlaceable(false);

        this.setUseAction(event -> {
            Player p = event.getPlayer();
            Location l = TwinUsage.getLocationFromString(event.getItem().getItemMeta().getLore().get(2));
            Chest chest=TwinUsage.getChest(l);
            if(chest!=null){
                Inventory inv=chest.getInventory();
                //这里chest.getChunk().isLoaded()始终返回true，不清楚其原理
                chest.getChunk().load();
                p.openInventory(inv);
                p.playSound(p.getEyeLocation(), Sound.BLOCK_ENDER_CHEST_OPEN, 1F, 0.6F);
            }else{
                p.sendMessage("§c对应的箱子已被拆除，无法打开孪生箱，请重新在箱子坐标上放上箱子！");
            }
        });
    }
}
